﻿using Mes.Domain.BoundedContext;
using AutoMapper;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Mes.Infrastructure.Data.BoundedContext.Extensions;
using Mes.Infrastructure.Data.BoundedContext.RepositoryBase;
using Mes.Application.Common;
using Mes.Application.Common.Resource;
using Mes.Application.DTO.Inputs.MasterData;
using Mes.Application.DTO.Results.MasterData;
using Mes.Domain.BoundedContext.Entities.MasterData;
using Mes.Infrastructure.Data.BoundedContext.Repositories.MasterData;

namespace Mes.Application.Services.MasterData
{
    public class MeasuringUnitTypeService : IMeasuringUnitTypeService
    {
        private readonly IRepository<MeasuringUnitType> _measuringUnitTypeRepository;
        private readonly IMeasuringUnitTypeRepository _measuringUnitTypeRepositoryInterface;
        private readonly IMapper _mapper;
        public MeasuringUnitTypeService(IRepository<MeasuringUnitType> measuringUnitTypeRepository, IMapper mapper, IMeasuringUnitTypeRepository measuringUnitTypeRepositoryInterface)
        {
            if (measuringUnitTypeRepository == null || mapper == null || measuringUnitTypeRepositoryInterface == null)
            {
                throw new ArgumentNullException();
            }
            _measuringUnitTypeRepository = measuringUnitTypeRepository; //注入仓储类
            _mapper = mapper;
            _measuringUnitTypeRepositoryInterface = measuringUnitTypeRepositoryInterface;
        }

        /// <summary>
        /// 添加计量单位类型
        /// </summary>
        /// <param name="measuringUnitTypeInput"></param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        public async Task AddMeasuringUnitTypeAsync(MeasuringUnitTypeInput measuringUnitTypeInput)
        {
            if (await _measuringUnitTypeRepository.ExistsAsync(x => x.Type == measuringUnitTypeInput.Type || x.Code == measuringUnitTypeInput.Code))
            {
                throw new CustomException(Messages.MeasuringUnitTypeExist);
            }

            var measuringUnitType = _mapper.Map<MeasuringUnitType>(measuringUnitTypeInput);
            await _measuringUnitTypeRepository.AddAsync(measuringUnitType);
            int status = await _measuringUnitTypeRepository.SaveAsync();
            if (status <= 0)
            {
                throw new CustomException(Messages.SavingFailure);
            }
        }

        /// <summary>
        /// 修改计量单位类型
        /// </summary>
        /// <param name="measuringUnitTypeUpdateInput"></param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        public async Task UpdateMeasuringUnitTypeAsync(MeasuringUnitTypeUpdateInput measuringUnitTypeUpdateInput)
        {
            var measuringUnitType = _measuringUnitTypeRepository.Find(x => x.Id == measuringUnitTypeUpdateInput.Id);
            if (measuringUnitType == null)
            {
                throw new CustomException(Messages.MeasuringUnitTypeNotFound);
            }
            measuringUnitType.UpdateTime = DateTime.Now;
            measuringUnitType.Type = measuringUnitTypeUpdateInput.Type;
            measuringUnitType.Code = measuringUnitTypeUpdateInput.Code;
            measuringUnitType.Note = measuringUnitTypeUpdateInput.Note;
            _measuringUnitTypeRepository.Update(measuringUnitType);
            int status = await _measuringUnitTypeRepository.SaveAsync();
            if (status <= 0)
            {
                throw new CustomException(Messages.SavingFailure);
            }
        }

        /// <summary>
        /// 删除计量单位类型
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="CustomException"></exception>
        public async Task DeleteMeasuringUnitTypeAsync(int[] ids)
        {
            Expression<Func<MeasuringUnitType, bool>> filter = x => ids.Contains(x.Id);
            if (filter == null)
            {
                throw new CustomException(Messages.MeasuringUnitTypeNotFound);
            }
            await _measuringUnitTypeRepository.DeleteAsync(filter);
            int status = await _measuringUnitTypeRepository.SaveAsync();
            if (status <= 0)
            {
                throw new CustomException(Messages.SavingFailure);
            }
        }

        /// <summary>
        /// 根据主键编号查询计量单位类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns>MeasuringUnitTypeResult</returns>
        /// <exception cref="CustomException"></exception>
        public async Task<MeasuringUnitTypeResult> GetMeasuringUnitTypeAsync(int id)
        {
            var measuringUnitType = await _measuringUnitTypeRepository.Query().Where(x => x.Id == id).FirstOrDefaultAsync();
            if (measuringUnitType == null)
            {
                throw new CustomException(Messages.MeasuringUnitTypeNotFound);
            }
            return _mapper.Map<MeasuringUnitTypeResult>(measuringUnitType);
        }

        /// <summary>
        /// 分页查询计量单位类型
        /// </summary>
        /// <param name="measuringUnitTypePageInput"></param>
        /// <returns>PagedItemsData<MeasuringUnitTypeResult></returns>
        /// <exception cref="CustomException"></exception>
        public async Task<PagedItemsData<MeasuringUnitTypeResult>> GetMeasuringUnitTypesAsync(MeasuringUnitTypePageInput measuringUnitTypePageInput)
        {
            var measuringUnitTypes = await _measuringUnitTypeRepository.Query().Where(x => x.Type.Contains(measuringUnitTypePageInput.Type)).ToPagedAsync(measuringUnitTypePageInput.PagedInput);
            if (measuringUnitTypes == null)
            {
                throw new CustomException(Messages.MeasuringUnitTypeNotFound);
            }
            return _mapper.Map<PagedItemsData<MeasuringUnitTypeResult>>(measuringUnitTypes);
        }
    }
}
